<?php
/**
 * The controller to manage information of medicine.
 *
 */
class C_MedicalAgent extends Controller {
	
	public $template_mod;
	
	function C_MedicalAgent($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'MedicalAgent') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'admin';
		}
		$this->template_mod = $template_mod;				
		$this->view->path = 'medical_agent';
		$this->setLayout('admin');
	}
	
	public function actionManagement() {
		return $this->view->render('medical_agent_management.html');
	}
	
	public function actionListMedicalAgent() {
		$medicalAgentList = Celini::newORDO('MedicalAgent')->getEnableIssueList();
		if(count($medicalAgentList) > 0){
			$this->assign('medicalAgentList', $medicalAgentList);
		}
		return $this->view->render('list.html');
	}
	
	public function actionEditMedicalAgent() {
		$agentId = $_GET['service_id'];
        $agent = Celini::newORDO('MedicalAgent');
        if (isset($agentId) && $agentId != "") {
            $agent->setup($agentId);
        }
        
        $this->assign_by_ref('agent', $agent);
        return $this->view->render('edit_medical_agent.html');
	}
	
	public function actionSaveMedicalAgentAjax() {
	   $agent = Celini::newORDO('MedicalAgent');
	   if (isset($_POST['id']) && $_POST['id'] > 0) {
            $agent->setup($_POST['id']);
       }
       $id	=	$agent->get_id();
       $name		=	Utils::sanitizeText($_POST['name']);
       $data	=	array();
       if($id > 0){
			$field		=	L10nHelper::getLocale();
			$field		=	L10nHelper::changeField($field, "name");
			$data[$field]	=	$name;
       }
       // Add new localized name
       else{
			$supported	=	L10nHelper::getSupportedLocales();
			foreach ($supported as $locale	=>	$country){
				$locale		= L10nHelper::changeField($locale, "name");
				$data[$locale]	=	$name;
			}
       }
    
	   $agent->populateArray($data);
	   $agent->persist();	   
	   
	   return $agent->get_id();
	}
	
	public function actionDeleteAgentAjax() {
		$agent = Celini::newORDO('MedicalAgent');
		$agent->disableAgent($_GET['agentId']);
	}
}
?>
